package indi.juzhuohan.chatroom.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.JOptionPane;
/**
 * 根据socket管道进行io操作
 * @author 鞠卓瀚
 *
 */
public class IOStream {
	/**
	 * 根据socket管道读取对象
	 * @param socket
	 * @return
	 */
	public static Object readMessage(Socket socket) {
		Object obj = null;
		try {
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);
			obj = ois.readObject();
		} catch (ClassNotFoundException e) {
			System.out.println("ios.read1");
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "服务器关闭", "失去连接",JOptionPane.WARNING_MESSAGE);
		}
		return obj;
	}
	/**
	 * 根据socket管道写信息
	 * @param socket
	 * @param message
	 */
	public static void writeMessage(Socket socket , Object message) {
		try {
			OutputStream os = socket.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(os);
			oos.writeObject(message);
			oos.flush();
		} catch (IOException e) {
			System.out.println("ios.write");
		}
	}
}
